package com.nsgf.library.ui.product;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.ConfirmOrderBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.utils.StringUtil;
import com.nsgf.library.utils.ToastUtils;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.nsgf.library.data.bean.LibraryInfo;
import com.zhht.htzx.databinding.ActivityCouponListBinding;
import com.zhht.htzx.databinding.ItemCouponBinding;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;

import java.util.ArrayList;
import java.util.List;

public class CouponListActivity extends BaseActivity {

    private CouponListStates mStates;

    private SimpleDataBindingAdapter<ConfirmOrderBean.CouponsDTO, ItemCouponBinding> mAdapter;

    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(CouponListStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<ConfirmOrderBean.CouponsDTO, ItemCouponBinding>(this, R.layout.item_coupon, new DiffUtil.ItemCallback<ConfirmOrderBean.CouponsDTO>() {
            @Override
            public boolean areItemsTheSame(@NonNull ConfirmOrderBean.CouponsDTO oldItem, @NonNull ConfirmOrderBean.CouponsDTO newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull ConfirmOrderBean.CouponsDTO oldItem, @NonNull ConfirmOrderBean.CouponsDTO newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemCouponBinding binding, ConfirmOrderBean.CouponsDTO item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.activity_coupon_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    private ActivityCouponListBinding mBinding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getLifecycle().addObserver(mAccountRequester);
        mBinding = (ActivityCouponListBinding) getBinding();
        mStates.titleBar.set("优惠券");
        mBinding.recyclerView.setItemAnimator(null);
        mAccountRequester.queryCoupons();

        mAccountRequester.getCouponList().observe(this, new Observer<ResponseStatus<List<ConfirmOrderBean.CouponsDTO>>>() {
            @Override
            public void onChanged(ResponseStatus<List<ConfirmOrderBean.CouponsDTO>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {
                    mStates.list.set(listResponseStatus.getData());
                }
            }
        });
        mAdapter.setOnItemClickListener((viewId, item, position) -> {
            if (0 < item.getStatus()) {
                startActivity(new Intent()
                        .setClass(CouponListActivity.this, NewComersActivity.class));
                finish();
            } else {
                JsonObject params = new JsonObject();
                params.addProperty("templateId", item.getTemplateId());
                params.addProperty("takeType", item.getTakeType());
                mAccountRequester.receiveTake(params);
            }
        });

        mAccountRequester.getSrcSuccess().observe(this, new Observer<ResponseStatus<String>>() {
            @Override
            public void onChanged(ResponseStatus<String> stringResponseStatus) {
                if (stringResponseStatus.getResponseCode() == 0) {
                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                    startActivity(new Intent()
                            .setClass(CouponListActivity.this, NewComersActivity.class));
                    ToastUtils.showShortToast(stringResponseStatus.getMsg());
                    finish();
                }
            }
        });
    }

    public static class CouponListStates extends StateHolder {

        public final State<List<ConfirmOrderBean.CouponsDTO>> list = new State<>(new ArrayList<>());
    }
}
